<?php
$template = "config-compte";
$reload=false;

function cleanString($string){
    $string = str_replace('http://', '', $string);
    if(substr($string, -1) === '/') $string = substr ($string, 0, -1);
    return $string;
}

// récupération des données en cours
if(DEBUG_MODE && file_exists(CONFIG_PATH.'config_local.ini')){
    $datas = parse_ini_file(CONFIG_PATH.'config_local.ini',true);
}elseif(!DEBUG_MODE && file_exists(CONFIG_PATH.'config.ini')){
    $datas = parse_ini_file(CONFIG_PATH.'config.ini',true);
}else{
    exit;
}

// suvegarde des données en tmp
if(isset($_POST)&&isset($_POST['submit'])){
    $datas['admin']['name'] = $_POST['admin_name'];
    $datas['admin']['mail'] = $_POST['admin_mail'];
    $datas['contact']['name'] = $_POST['contact_name'];
    $datas['contact']['mail'] = $_POST['contact_mail'];
    $datas['support']['name'] = $_POST['support_name'];
    $datas['support']['mail'] = $_POST['support_mail'];
    $datas['service']['name'] = $_POST['service_name'];
    $datas['service']['mail'] = $_POST['service_mail'];
    
    $config_ini = '';
    foreach($datas as $rub=>$content){
        $config_ini .= '['.$rub.']'."\n";
        foreach($content as $k=>$v){
            $config_ini .= $k.' = "'.$v.'"'."\n";
        }
    }
    
    $fileName = (DEBUG_MODE)?'config_local.ini':'config.ini';
    $handle = fopen(CONFIG_PATH.$fileName,"w+");
    fwrite($handle, $config_ini);
    fclose($handle);
    
    $reload = true;
}

// matching des données du .ini dans le formulaire
if(isset($datas)){
    foreach($datas as $rub=>$content){
        if($rub === 'langues'){
            $_POST[$rub]=$content;
        }else{
            foreach($content as $k=>$v){
                $_POST[$rub.'_'.$k] = cleanString($v);
            }
        }
    }
}

if($reload){
    header('location: ?template='.$template);
    exit;
}
?>
<div class="columns sixteen">
    <form method="post" enctype="multipart/form-data">
        <h2>Comptes de messagerie</h2>
        <table class="liste ui-widget" width="100%">
            <thead class="ui-widget-header">
                <tr>
                    <th>Type</th>
                    <th>Nom</th>
                    <th>E-mail</th>
                </tr>
            </thead>
            <tbody class="ui-widget-content">
                <tr>
                    <td>Admin</td>
                    <td><input name="admin_name" value="<?php echo (isset($_POST)&&isset($_POST['admin_name']))?$_POST['admin_name']:''; ?>"/></td>
                    <td><input name="admin_mail" value="<?php echo (isset($_POST)&&isset($_POST['admin_mail']))?$_POST['admin_mail']:''; ?>"/></td>
                </tr>
                <tr>
                    <td>Contact</td>
                    <td><input name="contact_name" value="<?php echo (isset($_POST)&&isset($_POST['contact_name']))?$_POST['contact_name']:''; ?>"/></td>
                    <td><input name="contact_mail" value="<?php echo (isset($_POST)&&isset($_POST['contact_mail']))?$_POST['contact_mail']:''; ?>"/></td>
                </tr>
                <tr>
                    <td>Support</td>
                    <td><input name="support_name" value="<?php echo (isset($_POST)&&isset($_POST['support_name']))?$_POST['support_name']:''; ?>"/></td>
                    <td><input name="support_mail" value="<?php echo (isset($_POST)&&isset($_POST['support_mail']))?$_POST['support_mail']:''; ?>"/></td>
                </tr>
                <tr>
                    <td>Service</td>
                    <td><input name="service_name" value="<?php echo (isset($_POST)&&isset($_POST['service_name']))?$_POST['service_name']:''; ?>"/></td>
                    <td><input name="service_mail" value="<?php echo (isset($_POST)&&isset($_POST['service_mail']))?$_POST['service_mail']:''; ?>"/></td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="3" align="right"><input class="pwe-ui-btn" type="submit" name="submit" value="Enregistrer" /></td>
                </tr>
            </tfoot>
        </table>
    </form>
</div>
<div class="columns sixteen">
    <hr style="margin: 15px 0;" />
</div>
<div class="columns sixteen">
    <h2>Membres admin</h2>
    <div align="right">
        <a class="pwe-ui-btn" href="javascript:openAddUser();">Ajouter</a>
    </div>
    <br/>
    <table class="liste ui-widget table_sortable" width="100%">
        <thead class="ui-widget-header">
            <tr>
                <th>Nom</th>
                <th>Accès</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody class="ui-widget-content">
            <?php $liste = modele_user::getListeAdmin(modele_user::getConnectedAdmin()->getLevel()); ?>
            <?php if(count($liste)>0): ?>
            <?php foreach($liste as $user): ?>
            <?php if($user->getLevel()<3): ?>
            <tr>
                <td><?php echo $user->getPrenom().' '.$user->getNom(); ?></td>
                <td><?php echo $user->getLevelNom(); ?></td>
                <td>
                    <a class="pwe-ui-btn" href="javascript:openUpdateUser(<?php echo $user->getId(); ?>);"><span class="ui-icon ui-icon-pencil"></span></a>
                    <a href="javascript:removeUser(<?php echo $user->getId(); ?>);" class="confirmAction pwe-ui-btn" title="Supprimer le membre '<?php echo $user->getPrenom().' '.$user->getNom(); ?>'"><span class="ui-icon ui-icon-trash"></span></a>
                </td>
            </tr>
            <?php endif; ?>
            <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
</div>